<?php
	
require_once("DBMySQL.class.php");

function getQuests($user_id) {
	$quests = array();
	
	try {
		$oDB = new DBMySQL();
		//$result = $oDB->query("SELECT * from quests ON (userlist.id = quests.user_id) where userlist.user_nm = '$user_nm' ");
		$result = $oDB->query("SELECT * from quests where user_id = '$user_id'");
		
		if( $result == false) {
			throw new Exception('getQuests: SELECT query is failed.');
		}
		
		for( $i = 0; $row = $result->fetch_assoc(); $i++ ) {
            $quest = array( 'quest_id' => $row['id'],
            			   'quest_nm' => $row['quest_nm'],
            			   'user_id' => $row['user_id'],
            			   'cat_id' => $row['cat_id'],
            			   'quest_type' => $row['quest_type'],
            			   'quest_start_date' => $row['quest_start_date'],
            			   'quest_exp_date' => $row['quest_exp_date'],
                           'quest_freq' => $row['quest_freq'],
                           'last_chk_date' => $row['last_chk_date'] );
            array_push($quests, $quest);
        }
		
		$oDB->close();
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	
	return json_encode( $quests );
}

function getQuestInfoPHP($quest_id) {
	
	$ret = array('result' => 'fail');
	
	try {
		$oDB = new DBMySQL();
		//$result = $oDB->query("SELECT * from quests ON (userlist.id = quests.user_id) where userlist.user_nm = '$user_nm' ");
		//$result = $oDB->query("SELECT * from quests where id = '$quest_id'");
		$result = $oDB->query("SELECT A.quest_nm, A.id,
								A.quest_type, A.quest_freq, B.cat_nm,  
								A.quest_start_date, A.quest_exp_date, 
								A.last_chk_date
						 		from quests A LEFT JOIN categorys B
						 		ON (A.cat_id = B.id) 
						 		where A.id = '$quest_id'");
		
		if( $result == false) {
			throw new Exception('getQuestInfoPHP: SELECT query is failed.');
		}
		
		$count = mysqli_num_rows($result);
		if($count == 1)
		{
			$row = $result->fetch_assoc();
			$ret = array('result' => 'success',
						'quest_id' => $row['id'],
						'quest_nm' => $row['quest_nm'],
						'cat_nm' => $row['cat_nm'], 
						'quest_type' => $row['quest_type'],
						'quest_freq' => $row['quest_freq'],
						'quest_start_date' => $row['quest_start_date'],
            			'quest_exp_date' => $row['quest_exp_date'],
            			'last_chk_date' => $row['last_chk_date'] );
		}
		
		$oDB->close();
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	return $ret;
}
	
function registerQuest($user_id, $quest_nm, $quest_type, $quest_freq, $quest_exp_date)
{
	$ret = array('result' => 'fail');
	
	try {
		$oDB = new DBMySQL();
		
		$prepare = $oDB->prepare("INSERT INTO quests (user_id, quest_nm, quest_type, quest_start_date, quest_exp_date, quest_freq) 
						values (?, ?, ?, ?, ?, ?)");
		
		if($prepare == false) {
			throw new Exception('registerQuest: Prepare INSERT query is failed.');
		}
		
		$prepare->bind_param('sssssi',
						$user_id,
						$quest_nm,
						$quest_type,
						date('Y-m-d'),
						$quest_exp_date,
						$quest_freq	);
		
		$result = $prepare->execute();
				
		if( $result == false) {
			throw new Exception('registerQuest: INSERT query is failed.');
		}
				
		$ret = array('result' => 'success' ); 

		
		$oDB->close();
		$oDB = null;
		
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	
	return json_encode( $ret );
	
}

function deleteQuest($quest_id)
{
	$ret = array('result' => 'fail');
	
	try {
		$oDB = new DBMySQL();
        
		$result = $oDB->query("DELETE from quests where id='$quest_id'");
		
		if( $result == false) {			
			throw new Exception('deleteQuest: DELETE query is failed.');
		}
		
		$ret = array('result' => 'success');
		
		$oDB->close();
		$oDB = null;
		
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	
	return json_encode( $ret );
	
}
	
?>